package reactive;

import java.util.ArrayList;
import java.util.List;

import mda.type.IAnyInterface;
import mda.type.IDtoType;
import mda.type.IEntityType;
import reactive.dojo.DojoAppBaseCtrl;
import reactive.dojo.DojoAppConstants;
import reactive.dojo.DojoAppLeftTree;
import reactive.dojo.DojoAppMain;
import reactive.dojo.DojoDaoService;
import reactive.dojo.DojoService;
import reactive.dojo.DojoServiceImpl;
import reactive.dojo.DojoTestServices;
import flca.mda.codegen.data.ITemplate;
import flca.mda.codegen.data.Template;
import flca.mda.codegen.data.TemplateMergeStrategy;

public class ReactiveDojoTemplates implements ReactiveConstantsBase  {
	
	public static final String REACTIVE_DOJO_BRANCH_NAME = "reactive.reactive.dojo";
	
	public static final String FRONTEND_DOJO = "dojoweb";

	protected static final String DOJO_WEB = LE + FRONTEND_DOJO + RI + "/js";

	protected static final String DOJO_DAO_SERVICE_CLSNAME = APPNAME + "DaoServices";
	
	private static List<ITemplate> allTemplates;
	
	public ReactiveDojoTemplates() {
		super();
		if (allTemplates == null) {
			allTemplates = makeTemplates();
		}
	}

	@Override
	public List<ITemplate> getAllTemplates() {
		return allTemplates;
	}

	public List<ITemplate> makeTemplates() {
		List<ITemplate> result = new ArrayList<ITemplate>();

		result.add(makeTemplate_DOJO_ENTITY());
		result.add(makeTemplate_DOJO_DTO());
		result.add(makeTemplate_DOJO_ENUM());
		result.add(makeTemplate_DOJO_ENTITY_DETAIL());
		result.add(makeTemplate_DOJO_ENTITY_BROWSE());
		result.add(makeTemplate_DOJO_ENTITY_SEARCH());
		result.add(makeTemplate_DOJO_ENTITY_CONTROLLER());
		result.add(makeTemplate_DOJO_WIDGET_HTML());
		result.add(makeTemplate_DOJO_WIDGET_JS());
		result.add(makeTemplate_DOJO_INDEX());
		result.add(makeTemplate_DOJO_CSS());
		
		result.add(makeDojoAppTemplate(Tid.DOJO_APP_CONSTANTS, APPNAME + "Constants", DojoAppConstants.class));
		result.add(makeDojoAppTemplate(Tid.DOJO_APP_MAIN, APPNAME + "Main", DojoAppMain.class));
		result.add(makeDojoAppTemplate(Tid.DOJO_APP_LEFTTREE, APPNAME + "LeftTree", DojoAppLeftTree.class));
		result.add(makeDojoAppTemplate(Tid.DOJO_APP_BASECTRL, APPNAME + "BaseCtrl", DojoAppBaseCtrl.class));
		
		result.add(makeTemplate_DOJO_APP_TREE_JSON());
		result.add(makeTemplate(Tid.DOJO_SERVICE, PCK, CLS, DojoService.class, "Service", SERVICES));
		result.add(makeTemplate(Tid.DOJO_SERVICE_IMPL, PCK, CLS + "Impl", DojoServiceImpl.class, "ServiceImpl", SERVICES));
		result.add(makeTemplate(Tid.DOJO_DAO_SERVICE, 	APP_PACKAGE + ".srv", DOJO_DAO_SERVICE_CLSNAME, 
				DojoDaoService.class, "DaoService", ENTITIES));
		result.add(makeTemplate(Tid.DOJO_TEST_SERVICES, APP_PACKAGE + ".srv", "Test" + CLS, 
				DojoTestServices.class, "TestServices", SERVICES));

		return result;
	}

	private ITemplate makeTemplate(Tid aTid, String aPkg, String aClassname, Class<?> aJetclass, 
			String aJetname, Class<?> appliesTo[]) {
		Template r = new Template();
		r.setName(aTid.name());
		r.setPackage(aPkg);
		r.setClassname(aClassname);
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(aJetclass.getName());
		r.setJetPath("/frontend/dojo/" + aJetname + ".jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SIMPLE_MERGE);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(appliesTo);
		r.setRank(5);
		return r;
	}
	
	private ITemplate makeDojoAppTemplate(Tid aTid, String aClassname, Class<?> aJetClass) {
		Template r = new Template();
		r.setName(aTid.name());
		r.setPackage(APP_PACKAGE);
		r.setClassname(aClassname);
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(aJetClass.getName());
		r.setJetPath("/frontend/dojo/" + aTid.name() +  ".jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IAnyInterface.class,ReactiveWebApp.class});
		r.setRank(5);
		return r;
	}

	


	private ITemplate makeTemplate_DOJO_ENTITY() {
		Template r = new Template();
		r.setName(Tid.DOJO_ENTITY.name());
		r.setPackage(PCK);
		r.setClassname(CLS + "");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoEntity.class.getName());
		r.setJetPath("/frontend/dojo/Entity.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SIMPLE_MERGE);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_DTO() {
		Template r = new Template();
		r.setName(Tid.DOJO_DTO.name());
		r.setPackage(PCK);
		r.setClassname(CLS + "");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoDto.class.getName());
		r.setJetPath("/frontend/dojo/Dto.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SIMPLE_MERGE);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_ENUM() {
		Template r = new Template();
		r.setName(Tid.DOJO_ENUM.name());
		r.setPackage(PCK);
		r.setClassname(CLS + "");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoEnum.class.getName());
		r.setJetPath("/frontend/dojo/Enum.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SIMPLE_MERGE);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {Enum.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_ENTITY_DETAIL() {
		Template r = new Template();
		r.setName(Tid.DOJO_ENTITY_DETAIL.name());
		r.setPackage(PCK + "view");
		r.setClassname(CLS + "DetailForm");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoDetailForm.class.getName());
		r.setJetPath("/frontend/dojo/DetailForm.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class,IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_ENTITY_BROWSE() {
		Template r = new Template();
		r.setName(Tid.DOJO_ENTITY_BROWSE.name());
		r.setPackage(PCK + "view");
		r.setClassname(CLS + "BrowseForm");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoBrowseForm.class.getName());
		r.setJetPath("/frontend/dojo/BrowseForm.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class,IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_ENTITY_SEARCH() {
		Template r = new Template();
		r.setName(Tid.DOJO_ENTITY_SEARCH.name());
		r.setPackage(PCK + "view");
		r.setClassname(CLS + "SearchForm");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoSearchForm.class.getName());
		r.setJetPath("/frontend/dojo/SearchForm.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class,IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_ENTITY_CONTROLLER() {
		Template r = new Template();
		r.setName(Tid.DOJO_ENTITY_CONTROLLER.name());
		r.setPackage(PCK);
		r.setClassname(CLS + "Controller");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoEntityController.class.getName());
		r.setJetPath("/frontend/dojo/EntityController.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class,IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_WIDGET_HTML() {
		Template r = new Template();
		r.setName(Tid.DOJO_WIDGET_HTML.name());
		r.setPackage(PCK + "widget");
		r.setClassname(CLS + "Widget");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".html");
		r.setGeneratorFqn(reactive.dojo.DojoWidgetHtml.class.getName());
		r.setJetPath("/frontend/dojo/widget-html.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class,IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_WIDGET_JS() {
		Template r = new Template();
		r.setName(Tid.DOJO_WIDGET_JS.name());
		r.setPackage(PCK + "widget");
		r.setClassname(CLS + "Widget");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".js");
		r.setGeneratorFqn(reactive.dojo.DojoWidgetJs.class.getName());
		r.setJetPath("/frontend/dojo/widget-js.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SIMPLE_MERGE);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IEntityType.class,IDtoType.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_APP_TREE_JSON() {
		Template r = new Template();
		r.setName(Tid.DOJO_APP_TREE_JSON.name());
		r.setPackage("resources");
		r.setClassname("tree");
		r.setTargetDir(DOJO_WEB);
		r.setFileExtension(".json");
		r.setGeneratorFqn(reactive.dojo.DojoAppTreeJson.class.getName());
		r.setJetPath("/frontend/dojo/widget-js.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {IAnyInterface.class,ReactiveWebApp.class});
		r.setRank(5);
		return r;
	}
	
	private ITemplate makeTemplate_DOJO_INDEX() {
		Template r = new Template();
		r.setName(Tid.DOJO_APP_INDEX.name());
		r.setPackage("");
		r.setClassname("index");
		r.setTargetDir(LE + FRONTEND_DOJO + RI);
		r.setFileExtension(".html");
		r.setGeneratorFqn(reactive.dojo.DojoIndexHtml.class.getName());
		r.setJetPath("/frontend/dojo/IndexHtml.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {ReactiveWebApp.class,});
		r.setRank(5);
		return r;
	}

	private ITemplate makeTemplate_DOJO_CSS() {
		Template r = new Template();
		r.setName(Tid.DOJO_APP_CSS.name());
		r.setPackage("");
		r.setClassname("style");
		r.setTargetDir(LE + FRONTEND_DOJO + RI + "/style");
		r.setFileExtension(".css");
		r.setGeneratorFqn(reactive.dojo.DojoCssStyle.class.getName());
		r.setJetPath("/frontend/dojo/CssStyle.jsjet");
		r.setMergeStrategy(TemplateMergeStrategy.SKIP);
		r.setCartridgeName(REACTIVE_CARTRIDGE_NAME);
		r.setApplyToClasses(new Class<?> [] {ReactiveWebApp.class,});
		r.setRank(5);
		return r;
	}

	

	public static ITemplate getTemplate(Tid aTid) {
		if (allTemplates == null) {
			new ReactiveDojoTemplates();
		}
		
		for (ITemplate t : allTemplates) {
			if (t.getName().equals(aTid.name())) {
				return t;
			}
		}
		return null;
	}


}
